const { app, shell, ipcMain, ipcRenderer } = require('electron')
const Store = require('electron-store')
const settingsStore = new Store()
function SavedFile(params) {

    return params.map(res => {
        return {
            label: res,
            click: (menuItem, browserWindow, event) => {
                browserWindow.webContents.send('New-file', res)
            }
        }
    })

}
const template = () => {
    let OldSavedFileLocation = settingsStore.get('OldSavedFileLocation') || []
    let Melabel = SavedFile(OldSavedFileLocation)
    let template = [{
        label: '文件',
        submenu: [
            {
                label: '打开文件夹',
                accelerator: 'CmdOrCtrl+F',
                click: (menuItem, browserWindow, event) => {
                    browserWindow.webContents.send('save-New-file')
                }
            },
            {
                label: '打开历史文件夹',
                submenu: Melabel
                // click: (menuItem, browserWindow, event) => {
                //   browserWindow.webContents.send('save-New-file')
                // }
            },
            {
                label: '编辑',
                accelerator: 'CmdOrCtrl+Z',
                click: (menuItem, browserWindow, event) => {

                    browserWindow.webContents.send('create-Edit-file') //发送给子进程
                }
            }, {
                label: '保存',
                accelerator: 'CmdOrCtrl+S',
                click: (menuItem, browserWindow, event) => {

                    browserWindow.webContents.send('save-edit-file')
                }
            }]
    },
    {
        label: '管理',
        submenu: [
            {
                label: '重启node',

                click: () => {
                    ipcMain.emit('end-node')
                }
            },
            {
                label: '刷新',
                accelerator: 'F5',
                role: 'reload'
             
            },
            {
                label: '强制刷新',
                accelerator: 'CmdOrCtrl+F5',
                role: 'forcereload'
             
            },
            {
                label: '开发者模式',

                accelerator: 'CmdOrCtrl+F1',
                role: 'toggledevtools'
            }
        ]
    },
    {
        label: '窗口',
        role: 'window',
        submenu: [
            {
                label: '最大化',
                accelerator: 'CmdOrCtrl+N',
                role: 'togglefullscreen'
            },
            {
                label: '最小化',
                accelerator: 'CmdOrCtrl+M',
                role: 'minimize'
            }, {
                label: '关闭',
                accelerator: 'CmdOrCtrl+W',
                role: 'close'
            }]
    },
    {
        label: '帮助',
        role: 'help',
        submenu: [
            {
                label: '学习更多',
                click: () => { shell.openExternal('http://electron.atom.io') }
            },
        ]
    },
    ]

    if (process.platform === 'darwin') {
        const name = app.getName()
        template.unshift({
            label: name,
            submenu: [{
                label: `关于 ${name}`,
                role: 'about'
            }, {
                type: 'separator'
            }, {
                label: '设置',
                accelerator: 'Command+,',
                click: () => {
                    ipcMain.emit('open-settings-window')
                }
            }, {
                label: '服务',
                role: 'services',
                submenu: []
            }, {
                type: 'separator'
            }, {
                label: `隐藏 ${name}`,
                accelerator: 'Command+H',
                role: 'hide'
            }, {
                label: '隐藏其它',
                accelerator: 'Command+Alt+H',
                role: 'hideothers'
            }, {
                label: '显示全部',
                role: 'unhide'
            }, {
                type: 'separator'
            }, {
                label: '退出',
                accelerator: 'Command+Q',
                click: () => {
                    app.quit()
                }
            }]
        })
    } else {
        //加到文件的最后一个
        // template[0].submenu.push({
        //   label: '设置',
        //   accelerator: 'Ctrl+,',
        //   click: () => {
        //     ipcMain.emit('open-settings-window')
        //   }
        // })
    }
    return template
}


module.exports = template
